= field_set do
  - if params[:nature]
    = f.input :nature, as: :hidden
  - else
    = f.input :nature
  = f.input :number
  = f.input :reference_number
  = f.input :sampled_at
  - unless params[:without_geolocation].to_i > 0
    = f.geolocation
  = f.referenced_association :sampler
  = f.input :analysed_at
  = f.referenced_association :analyser
  = f.referenced_association :product

= field_set :items do
  :ruby
    if params[:nature] and nature = Nomen::AnalysisNature.find(params[:nature])
      unless nature.items.nil?
        nature.items.each do |item|
          next unless indicator = Nomen::Indicator.find(item.to_s)
          unless f.object.items.detect{|i| i.indicator_name.to_s == item.to_s}
            attrs = {indicator_name: item}
            if indicator.datatype == :measure
              attrs[:measure_value_unit] = indicator.unit
            end
            f.object.items.build(attrs)
          end
        end
      end
    end

  .nested-association#analysis-items
    = f.backend_fields_for(:items) do |i|
      = render "backend/analysis_items/form", f: i

    .links
      :ruby
        existing = f.object.items.map(&:indicator_name).map(&:to_s)
        indicator_values = Nomen::Indicator.selection
        indicator_values.each { |elem| elem << { disabled: true, hidden: true } if existing.include?(elem.last) }
      %span.nested-add= :add_item.tl
      %span.input-append= select_tag(:indicator_name, options_for_select(indicator_values), id: 'indicator-selector')
      = link_to(:add.tl, { action: :new, controller: :analysis_items }, class: "btn btn-default", remote: true, data: { update: '.links', update_at: 'before', closest: '#analysis-items', with: '#indicator-selector' })

= f.attachments_field_set
